from Factors import *
# from sklearn.linear_model import LinearRegression

def Automatic_UpdateInstrumentFactors(database, datetime2, calculate):
    #
    datetime1 = datetime.datetime(2000, 1, 1)
    #
    # datetime1 = Gadget.ToUTCDateTime(datetime1)
    # datetime2 = Gadget.ToUTCDateTime(datetime2)
    CalcInstrumentFactors(database, datetime1, datetime2, forceToUpdate=False, calculate=calculate)


def CalcInstrumentFactors(database, datetime1, datetime2, forceToUpdate=True, calculate=["Daily","Monthly","Quarterly"]):

    update = True
    if forceToUpdate:
        update = False

    stocks = database.Find("Instruments", "Stock")
    indexs = database.Find("Instruments", "Index")
    # stocks = database.find("Instruments", "Stock", query={"limit": 10})
    # stocks = database.find("Instruments", "Stock", query={"Symbol": "000001.SZ"})
    totalInstrument = indexs + stocks
    hs300 = database.Find("Instruments", "Index", filter={"Symbol": "000300.SH"})
    shanghai = database.Find("Instruments", "Index", filter={"Symbol": "000001.SH"})

    # ---Return---
    # if "Monthly" in calculate:
    #     CalcMonthlyReturn(database, totalInstrument, datetime1, datetime2, update=update)
    # if "Daily" in calculate:
    #     CalcReturn(database, totalInstrument, datetime1, datetime2, update=update)

    if "Daily" in calculate:
        # ---Size---
        CalcCapFactor(database, stocks, datetime1, datetime2, update=update)
        # # ---Price---
        CalcPriceFactor(database, stocks, datetime1, datetime2, update=update)
        # # ---Market Model / Alpha Beta Volatility ---
        CalcCAPModel(database, stocks, datetime1, datetime2, update=update)
        # ---Volatility---
        CalcVolatilityFactor(database, totalInstrument, datetime1, datetime2, update=update)
        pass

    if "Monthly" in calculate:
        # ---Valuation---
        CalcValuatioFactor(database, stocks, datetime1, datetime2, update=update)
        pass

    if "Quarterly" in calculate:
        # ---Profitability---
        CalcProfitabilityFactor(database, stocks, datetime1, datetime2, update=update)
        # ---CashFlow---
        CalcCashFlowFactor(database, stocks, datetime1, datetime2, update=update)
        # ---Historical Growth---
        CalcGrowthFactor(database, stocks, datetime1, datetime2, update=update)
        # ---Capital Allocation---
        # CalcCapitalAllocationFactor(database, stocks, datetime1, datetime2, update=update)
        # ---Operating Flag---
        # CalcOprationFactor(database, stocks, datetime1, datetime2, update=update)
        pass

    # ---Liquidity---

    # ---Share Holder---

    # ---Market Style Base on Cap / PB(Valuation)---
    # CalcMarketStyle(database, instruments, datetime1, datetime2)


def CalcInstrumentFactor(datetime1, datetime2, instrument, factorName,  update=False):
    pass


def CalcProfileFactors(instrments, datetime1, datetime2):
    print("CalcProfileFactors")


def CalcLabels(instrments, datetime1, datetime2):
    print("CalcLabels")


def BuildIndex(database, factorName):
    database.creatIndex("Facotr", factorName, "Key")
    database.creatIndex("Facotr", factorName, "Symbol")
    database.creatIndex("Facotr", factorName, "StdDateTime")


def AddFactorsIndex(database):
    names = database.GetCollectionNames("Factor")

    for name in names:
        database.creatIndex("Factor", name, "Key")
        database.creatIndex("Factor", name, "Symbol")
        database.creatIndex("Factor", name, "DateTime")
        database.creatIndex("Factor", name, "StdDateTime")
        database.creatIndex("Factor", name, "Date")


if __name__ == '__main__':
    #
    from Core.Config import *
    config = Config()
    database = config.DataBase("MySQL")

    # AddFactorsIndex(database)

    # ---DateTime---
    datetime1 = datetime.datetime(2000, 1, 1)
    # datetime1 = datetime.datetime(2018, 12, 30)

    datetime1 = datetime.datetime(2019, 5, 1)
    # datetime2 = datetime.datetime(2020, 1, 1)
    datetime2 = datetime.datetime(2019, 9, 17)

    #
    # CalcInstrumentFactors(datetime1, datetime2, forceToUpdate=True, calculate=["Monthly"])
    Automatic_UpdateInstrumentFactors(database, datetime2, calculate=["Quarterly"]) #"Monthly","Quarterly" "Daily",

    stocks = database.Find("Instruments", "Stock")
    indexs = database.Find("Instruments", "Index2")
    totalInstrument = stocks + indexs

    # CalcCashFlowFactor(database, stocks, datetime1, datetime2, update=False)
    # CalcVolatilityFactor(database, totalInstrument, datetime1, datetime2, update=False)
    # CalcPriceFactor(database, totalInstrument, datetime1, datetime2, update=True)
    # CalcReturn(database, totalInstrument, datetime1, datetime2, update=True)
    CalcValuatioFactor(database, stocks, datetime1, datetime2, update=True)

    #FindMostSuitableSeasonalReportAlignDate(database)
    #industries = Industries(instruments)
    #BuildFactorDatabase(database,instruments)
    #selectedInstrument = industries["交通运输"]

    #PlotFactor(database, "002530.SZ","BookToMarket")
    #df = LoadSingleFactor(database, "BookToMarket", datetime3)
    #df.to_csv("D://Data//DIYFinancialAdvisor//" + "test.csv")
    pass